# -*- coding: utf-8 -*-
'''
Created on Apr 10, 2013

@author: LONG HOANG GIANG
'''
import sys, os
from urlparse import urljoin
import traceback
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import pyLib
import logging
import json
import re

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S')


class Truyen8Mobi():
    
    __config_output = "/longhoanggiang/truyentext/"
    
    def getListChapter(self, url):
        tree = pyLib.loadWeb(url).build_tree()
        data = [{'name': "Giới Thiệu", "url": url, "path": pyLib.crc32unsigned(url)}]
        for node in tree.xpath("//ul[@class='form magb5']/li[last()]//option[position()>1]"):
            href = node.get('value')
            if href == '': continue
            href = urljoin(url, href)
            name = pyLib.stringify(node)
            print name, href
            data.append({'name': name, 'path': pyLib.crc32unsigned(href), 'url': href})
        return data
    
    def getDetail(self, item):
        logging.debug("get detail {0}".format(item['url']))
        url = item['url']
        partNum = 0
        breakCondition = False
        data = []
        while not breakCondition:
            try:
                partNum += 1
                chapId = pyLib.crc32unsigned(url)
                tree = pyLib.loadWeb(url).build_tree()
                pagesNode = tree.xpath("//div[@class='pages']/ul[@id='list_page']")
                if len(pagesNode) > 0:
                    nextNode = pagesNode[0].xpath(".//li/a[@class='active']/../following-sibling::*[1]/a")
                    if len(nextNode) > 0:
                        href = nextNode[0].get('href')
                        if href == '': breakCondition = True
                        url = urljoin(url, href)
                    else:
                        breakCondition = True
                else:
                    breakCondition = True
                name = "{0} - Phần {1}".format(item['name'], partNum) if len(pagesNode) > 0 else item['name']
                contentNode = tree.xpath("//div[contains(@class, 'news-dtail')]")[0]
                if len(pagesNode) > 0:
                    pyLib.Etree.cleanNodeNextSibling(tree.xpath("//div[@class='pages']"), True)
                ulNode = tree.xpath("//ul[@class='form magb5']")
                if len(ulNode) > 0:
                    pyLib.Etree.cleanNodePreceding(ulNode[0], True)
                for xp in ["//h2[@id='introtext']/preceding-sibling::*", "//div[@class='noitice']"]:
                    for node in tree.xpath(xp):
                        node.getparent().remove(node) 
                content = pyLib.cleanHTML(pyLib.Etree.tostring(contentNode)).strip()
                content = re.sub(r"\n", "<br />", content)
                content = '''<h4>{0}</h4><hr />{1}'''.format(name, content)
                pyLib.gzip(self.__config_output + "files/" + chapId, json.dumps(content))
                data.append({'name': name, 'path': chapId})
                print '-----------------------------------'
                print name
                print '-----------------------------------'
                print content
            except:
                traceback.print_exc()
        return data
    
    def process(self, url):
        output = pyLib.extractText(r"/([^/]+)\.html", url, 1)
        self.__config_output = self.__config_output + output + "/"
        pyLib.createIfNotExistsPath(self.__config_output)
        chapters = self.getListChapter(url)
        data = []
        for item in chapters:
            contents = self.getDetail(item)
            for cc in contents:
                data.append(cc)
        pyLib.gzip(self.__config_output + "data", pyLib.encryptCipher(json.dumps(data)))
            
if __name__ == '__main__':
    
    t = Truyen8Mobi()
    t.process('http://truyen8.mobi/t279-50-sac-thai-tap-1.html')
    logging.info("finished")
    os._exit(1)
        
        
        
        